/*
 * @(#)WebAspect.java    May 20, 2010
 *
 * Copyright 2009 Nyber Shanghai, Inc. All rights reserved.
 */

package com.common.aspect;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

/**
 * Web layer aspect.
 *
 * @author nickevin@gmail.com
 * @version Jun 2, 2009
 *
 */

//@Aspect
//@Component
public class WebAspect {

	private final static String SECURITY_URL = "/security/login";

	/**
	 * You must login to view authorized resource.  
	 * 
	 * @param jp
	 * 			ProceedingJoinPoint
	 * @param request
	 * 			HttpServletRequest
	 * @param response
	 * 			HttpServletResponse
	 * @return Object
	 * 			
	 * @throws Throwable
	 */
	@Around("execution(* *..web.*Controller.*(..)) && args(request, response,..)")
	public Object doLogin(ProceedingJoinPoint jp, HttpServletRequest request,
			HttpServletResponse response) throws Throwable {
		String currentUserName = SpringSecurityUtils.getCurrentUserName();
		if ("anonymousUser".equals(currentUserName)) {
			String contextPath = request.getContextPath();
			response.sendRedirect(contextPath + SECURITY_URL);

			return null;
		}

		return jp.proceed();
	}

}